package com.atguigu.gulimall.order.config;

import com.atguigu.gulimall.util.consts.MqConst;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author nicc
 * @version 1.0
 * @className OrderMqConfiguration
 * @date 2022-10-25 03:22
 */

@Configuration
public class OrderMqConfiguration {

    /**
     * 订单事件交换机
     * @return
     */
    @Bean
    public Exchange orderEventExchange(){
        TopicExchange topicExchange = new TopicExchange(MqConst.ORDER_EVENT_EXCHANGE,
                true,
                false);

        return topicExchange;
    }

    /**
     * 订单延迟队列
     * @return
     */
    @Bean
    public Queue orderDelayQueue(){

        //给延迟队列的参数项
        Map<String,Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange",MqConst.ORDER_EVENT_EXCHANGE);//死信交给那台交换机
        args.put("x-dead-letter-routing-key",MqConst.RK_ORDER_DELAY);//死信使用的路由键
        args.put("x-message-ttl",MqConst.ORDER_DELAY_TTL);//消息存活时间
        //args.put("x-message-ttl",60000);

        return new Queue(MqConst.ORDER_DELAY_QUEUE,
                true,
                false,
                false,args);
    }


    /**
     * 订单交换机和延迟队列绑定
     * @return
     */
    @Bean
    public Binding orderDelayBinding(){
        /**
         * String destination,  目的地
         * DestinationType destinationType, 目的地类型【队列、交换机】
         * String exchange,  交换机
         * String routingKey, 路由键
         * @Nullable Map<String, Object> arguments 参数项
         *
         * 把 exchange 和 destination 用 routingKey 进行绑定
         */
        return new Binding(MqConst.ORDER_DELAY_QUEUE,
                Binding.DestinationType.QUEUE,
                MqConst.ORDER_EVENT_EXCHANGE,
                MqConst.RK_ORDER_CREATED,
                null);
    }


    /**
     * 订单关闭队列
     * @return
     */
    @Bean
    public Queue orderCloseQueue(){
        return new Queue(MqConst.ORDER_CLOSE_QUEUE,
                true,false,false);
    }

    /**
     * 订单交换机和关单队列绑定
     */
    @Bean
    public Binding orderCloseBinding(){
        return new Binding(MqConst.ORDER_CLOSE_QUEUE,
                Binding.DestinationType.QUEUE,
                MqConst.ORDER_EVENT_EXCHANGE,
                MqConst.RK_ORDER_DELAY,
                null);
    }


    /**
     * 支付成功单队列
     * @return
     */
    @Bean
    public Queue payedQueue(){

        return new Queue(MqConst.ORDER_PAYED_QUEUE,true,false,false);
    }

    /**
     * 成功单队列绑定
     * @return
     */
    @Bean
    public Binding payedQueueBinding(){
        return new Binding(MqConst.ORDER_PAYED_QUEUE,
                Binding.DestinationType.QUEUE,
                MqConst.ORDER_EVENT_EXCHANGE,
                MqConst.RK_ORDER_PAYED,null);
    }
}
